package com.hntx.joymusic.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hntx.joymusic.R;
import com.hntx.joymusic.bean.VideoBean;

import java.util.List;

public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.MyHolder> {

    private List<VideoBean> list;
    private Context context;

    public MyListAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<VideoBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public OnRightFocusChangeListener onRightFocusChangeListener;
    public OnRightClickListener onRightClickListener;

    public interface OnRightFocusChangeListener {
        void onRightFocusChange(View view, boolean flag, int pos);
    }

    public interface OnRightClickListener {
        void onRightClick(View view, int pos);
    }

    public void addRightFocusChangeListener(OnRightFocusChangeListener onRightFocusChangeListener) {
        this.onRightFocusChangeListener = onRightFocusChangeListener;
    }

    public void addRightClickListener(OnRightClickListener onRightClickListener) {
        this.onRightClickListener = onRightClickListener;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.right_list_layout, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, int position) {
        holder.name.setText(list.get(position).getSongName());
        holder.score.setText(list.get(position).getSinger());
        holder.score.setTag(list.get(position).getSinger());
        if (holder.itemView.isSelected()) {
            holder.itemView.setBackgroundResource(R.mipmap.back);
        } else {
            holder.itemView.setBackground(null);
        }
        holder.del.setOnClickListener(v -> {
            if (onRightClickListener != null) {
                onRightClickListener.onRightClick(v, position);
            }
        });
        holder.play.setOnFocusChangeListener((v, hasFocus) -> {
            if (onRightFocusChangeListener != null) {
                onRightFocusChangeListener.onRightFocusChange(v, hasFocus, position);
            }
            if (hasFocus) {
                ((RelativeLayout) v.getParent()).setBackgroundResource(R.mipmap.back);
            } else {
                ((RelativeLayout) v.getParent()).setBackground(null);
            }
        });
        holder.del.setOnFocusChangeListener((v, hasFocus) -> {
            if (onRightFocusChangeListener != null) {
                onRightFocusChangeListener.onRightFocusChange(v, hasFocus, position);
            }
            if (hasFocus) {
                ((RelativeLayout) v.getParent()).setBackgroundResource(R.mipmap.back);
            } else {
                ((RelativeLayout) v.getParent()).setBackground(null);
            }
        });
        holder.play.setOnClickListener(v -> {
            if (onRightClickListener != null) {
                onRightClickListener.onRightClick(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size() > 0 ? list.size() : 0;
    }

    class MyHolder extends RecyclerView.ViewHolder {
        private ImageView play, del;
        private TextView name, score;

        public MyHolder(View itemView) {
            super(itemView);
            play = itemView.findViewById(R.id.start_play);
            del = itemView.findViewById(R.id.delete_m);
            name = itemView.findViewById(R.id.name);
            score = itemView.findViewById(R.id.score);
        }
    }
}
